package com.txh.blog.service.Impl;

import com.txh.blog.bean.Comment;
import com.txh.blog.dao.BlogDao;
import com.txh.blog.dao.CommentDao;
import com.txh.blog.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    private CommentDao commentDao;

    @Autowired
    private BlogDao blogDao;
    private List<Comment> comments;
    //存放迭代找出的所有子代的集合
    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {
        List<Comment> commentList = commentDao.selectRoot(blogId, null);
        List<Comment> commentList1 = eachComment(commentList);
        for (Comment commet:commentList1) {
            List<Comment> replyComments = commet.getReplyComments();
            for (int i = 0; i < replyComments.size(); i++) {
                for (int j = i+1; j < replyComments.size(); j++) {
                   if (replyComments.get(i).getId().equals(replyComments.get(j).getId())){
                       replyComments.remove(j);
                   }
                }
            }
        }

        return commentList1;
    }

    @Override
    public int saveComment(Comment comment) {
        //判断有没有在别人的评论上进行评论还是一个新的评论
        Long parentCommentId = comment.getParentComment().getId();
        Long blogId = comment.getBlogId();
        //没有父级评论默认是-1
        if (parentCommentId != -1) {
            //有父级评论
           comment.setCommentId(parentCommentId);
           comment.setReplyComments(commentDao.recursionChildren(parentCommentId,blogId));
          comment.setParentComment(commentDao.selectById(parentCommentId,blogId));
        } else {
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        int i = commentDao.saveComment(comment);

        return i;
    }
    /**
     * 循环每个顶级的评论节点
     * @param comments
     * @return
     */
    private List<Comment> eachComment(List<Comment> comments) {
        List<Comment> commentsView = new ArrayList<>();
        for (Comment comment : comments) {
            Comment c = new Comment();
            BeanUtils.copyProperties(comment,c);
            commentsView.add(c);
        }
        //合并评论的各层子代到第一级子代集合中
        combineChildren(commentsView);
        return commentsView;
    }

    /**
     *
     * @param comments root根节点，blog不为空的对象集合
     * @return
     */
    private void combineChildren(List<Comment> comments) {
        for (Comment comment : comments) {
            List<Comment> replys1 = comment.getReplyComments();
            for(Comment reply1 : replys1) {
                //循环迭代，找出子代，存放在tempReplys中
                recursively(reply1);
            }
            //修改顶级节点的reply集合为迭代处理后的集合
            comment.setReplyComments(tempReplys);
            //清除临时存放区
            tempReplys = new ArrayList<>();
        }
    }
    private List<Comment> tempReplys = new ArrayList<>();
    /**
     * 递归迭代，剥洋葱
     * @param comment 被迭代的对象
     * @return
     */
    public void recursively(Comment comment) {
        tempReplys.add(comment);//顶节点添加到临时存放集合
        if (comment.getReplyComments().size()>0) {
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply : replys) {
                tempReplys.add(reply);
                if (reply.getReplyComments().size()>0) {
                    recursively(reply);
                }
            }
        }
    }
}
